package com.vulb.activity;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.EditText;

import com.vulb.R;
import com.vulb.application.GlobalContext;
import com.vulb.logic.Login;
import com.vulb.logic.SessionManager;
import com.vulb.protocal.http.Response;

public class LoginActivity extends BasicActivity {
    private ProgressDialog mLoadingDialog;
    private LoginTask mLoginTask;
    
    EditText m_et_accont = null;
    EditText m_et_pass = null;
    String m_account = "";
    String m_pass = "";
    boolean is_logining = false;
    private void autoLogin()
    {
    	SharedPreferences info = this.getSharedPreferences(SessionManager.logged_user_info,0);
    	m_account = info.getString(SessionManager.current_user_account,"");
    	m_pass = info.getString(SessionManager.current_user_password,"");
    	if(!m_account.equals("") && !m_pass.equals("") && !is_logining)
    	{
    		mLoadingDialog = ProgressDialog.show(LoginActivity.this, "Login", "Login...");
    		mLoginTask = new LoginTask();
            mLoginTask.execute("");
    	}
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        View loginBtn = findViewById(R.id.btn_login);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) 
            {
            	if(is_logining)
            	{
            		return;
            	}
                mLoadingDialog = ProgressDialog.show(LoginActivity.this, "Login", "Login...");
                m_account = m_et_accont.getText().toString();
                m_pass = m_et_pass.getText().toString();
                mLoginTask = new LoginTask();
                mLoginTask.execute("");
            }
        });
        m_et_accont = (EditText)findViewById(R.id.et_login_accont);
        m_et_pass = (EditText)findViewById(R.id.et_login_pass);
        m_et_accont.setText("ifqqfi@gmail.com");
        m_et_pass.setText("123321");
        GlobalContext.getInstance().setContext(this);
        autoLogin();
    }
    
    private class LoginTask extends AsyncTask<String, Integer, Integer> {
        @Override
        protected Integer doInBackground(String... params) {
        	is_logining = true;
            Login loginLogic = new Login();
            int succ = loginLogic.login(m_account, m_pass);
            Message.obtain(mHttpResponseHandler, HTTP_RESPONSE_STATUS, succ, 0).sendToTarget();  
            if (succ != Response.SUCCESS) 
            {
                //TODO show login error
            }
            mLoadingDialog.cancel();
            is_logining = false;
            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
            mLoadingDialog.hide();
        }
    }
}
